#include "StdAfx.h"
#include "Log.h"
#include <stdio.h>

CLog sLog;
CLog::CLog(void)
{
	m_pFile = NULL;
	InitializeCriticalSection(&m_cs);
}

CLog::~CLog(void)
{
	if (m_pFile)
		fclose(m_pFile);
	DeleteCriticalSection(&m_cs);
}

bool CLog::Init(const wchar_t* path)
{
	SYSTEMTIME st;
	GetSystemTime(&st);
	wstring strPath = path;
	wchar_t wsBuff[20] = {0};
	wsprintf(wsBuff, _T("%d-%d-%d %d-%d.txt"), st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute);
	strPath += wsBuff;
	m_pFile = _wfopen(strPath.c_str(), _T("w+"));
	if (m_pFile == NULL)
	{
		assert(0);
		return 0;
	}
	return 1;
}

void CLog::Write(const char* szText)
{
	EnterCriticalSection(&m_cs);
	fputs(szText, m_pFile);
	LeaveCriticalSection(&m_cs);
}
